package datasv.courseProduce_datasv;

import data.datafactory.DatabaseOperator;
import po.CourseAssistPO;
import po.CourseBasicPO;
import resultmessage.CourseProduceRM;

public class AddCourseBasic {
	public static void main(String[] args) {
		//System.out.println(new AddCourseBasic().addCourseBasic(new CourseBasicPO("00106130020", "testB",5,3,"xianText","001","testD",120)));
	}
	protected static String[] columnName={"id","campus","department","credit","semester","maxStudent","name"};
	protected static int columnNum=columnName.length;
	public CourseProduceRM addCourseBasic(CourseBasicPO po){
		return addInSql(po);
	}
	public CourseProduceRM addInSql(CourseBasicPO po){
		String collegeID=po.getCollegeID();
		DatabaseOperator op=new DatabaseOperator("college"+collegeID);
		if(!op.ifDatabaseExist()||!op.ifIDExist("course_list", po.getCourseId())){
			return CourseProduceRM.COURSEBASIC_EXIT;
		}
		int num=columnNum;
		String[][] info=new String[num][2];
		info[0][0]=columnName[0];info[0][1]=po.getCourseId();
		info[1][0]=columnName[1];info[1][1]=po.getCampus();
		info[2][1]=po.getDepartment();info[2][0]=columnName[2];
		info[3][1]=Integer.toString(po.getCredit());info[3][0]=columnName[3];
		info[4][1]=Integer.toString(po.getSemester());info[4][0]=columnName[4];
		info[5][1]=Integer.toString(po.getMaxStudentNum());info[5][0]=columnName[5];
		info[6][1]=po.getCourseName();info[6][0]=columnName[6];
		
		for (int i = 0; i <num; i++) {
			try {
				op.getOneInform(po.getCollegeID(), "course_list", info[i][0])
						.equals(null);
				return CourseProduceRM.COURSEBASIC_EXIT;
			} catch (NullPointerException e) {
				if (!op.setOneInform(info[0][1], info[i][1], "course_list",
						info[i][0])) {
					return CourseProduceRM.COURSEBASIC_EXIT;
				}
			}
		}
		return CourseProduceRM.SUCCESS;
	}
}
